package my.jwds.p2043;

public class Bank {

    public static void main(String[] args) {

        Bank obj = new Bank(new long[]{10,100,20,50,30});
        obj.transfer(5,1,20);
        long start = System.currentTimeMillis();
        System.out.println("运行时间:" + (System.currentTimeMillis() - start));
    }


    long[]balance;
    public Bank(long[] balance) {
        this.balance = balance;
    }

    public boolean transfer(int account1, int account2, long money) {
        if (isValidAccount(account1) && isValidAccount(account2)){
            if (balance[account1-1] >= money){
                balance[account1-1] -= money;
                balance[account2-1] += money;
                return true;
            }
            return false;
        }
        return false;
    }

    public boolean deposit(int account, long money) {
        if (isValidAccount(account)){
            balance[account-1] += money;
            return true;
        }
        return false;
    }

    public boolean withdraw(int account, long money) {
        if (isValidAccount(account)){
            if (balance[account-1] >= money){
                balance[account-1] -= money;
                return true;
            }
            return false;
        }
        return false;
    }

    boolean isValidAccount(int account){
        return balance.length >= account && account >= 1;
    }

}